package dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import util.HibernateUtil;
import util.Util;
import vo.StrategicPlanVO;
import entity.OrganizationElement;
import entity.StrategicPlan;
import entity.Usuario;

public class StrategyDAO {
	
	public int insertStrategy(StrategicPlanVO vo) {	
		Session sessao = HibernateUtil.getSession();
		Transaction tx = sessao.beginTransaction();
		
		try{		
			StrategicPlan strategy = new StrategicPlan();
			strategy.setDataCriacao(vo.getDataCriacao());
			strategy.setDataFim(vo.getDataFim());
			strategy.setNome(vo.getNome());
			strategy.setDescricao(vo.getDescricao());
			
			UsuarioDAO usuarioDAO = new UsuarioDAO();		
			strategy.setUsuario(usuarioDAO.getUser(vo.getUsuarioId(), sessao));
			
			OrganizationElement oe = (OrganizationElement) sessao.load(OrganizationElement.class, vo.getOrganizacaoId());
			strategy.setOrganizacao(oe);
			
			sessao.saveOrUpdate(strategy);
			tx.commit();
			sessao.close();
			
			return strategy.getId();
		}
		catch(Exception e){
			e.printStackTrace();
			tx.rollback();
			sessao.close();
			
			return -1;
		}		
	}

	public List<StrategicPlanVO> listStrategy(int selectedOrganization, int idUsuario) {
		Session sessao = HibernateUtil.getSession();
		Criteria criterios = sessao.createCriteria(StrategicPlan.class);
		
		if (selectedOrganization > 0){
			OrganizationElement o = (OrganizationElement) sessao.load(OrganizationElement.class, selectedOrganization);
			criterios.add(Restrictions.eq("organizacao", o));
		}
		else {
			Usuario u = (Usuario) sessao.load(Usuario.class, idUsuario);
			criterios.add(Restrictions.in("organizacao", u.getOrganizations()));
		}
		
		List<?> lista = criterios.list();
		List<StrategicPlanVO> listaStrategicPlan = new ArrayList<StrategicPlanVO>();
		
		for(Object obj:lista){
			StrategicPlan sp = (StrategicPlan) obj;
			StrategicPlanVO vo = new StrategicPlanVO();
			
			vo.setNome(sp.getNome());
			vo.setDescricao(sp.getDescricao());
			vo.setDataCriacao(sp.getDataCriacao());
			vo.setDataFim(sp.getDataFim());
			vo.setNomeOrganizacao(sp.getOrganizacao().getName());
			vo.setDataCriacaoString(Util.convertDateToString(sp.getDataCriacao()));
			vo.setDataFimString(Util.convertDateToString(sp.getDataFim()));
			vo.setOrganizacaoId(sp.getOrganizacao().getId());
			vo.setStrategicPlanId(sp.getId());
			
			listaStrategicPlan.add(vo);			
		}
		
		sessao.close();
		return listaStrategicPlan;	
	}

	public void updateStrategicPlan(StrategicPlanVO vo) {
		Session sessao = HibernateUtil.getSession();
		StrategicPlan strategicPlan = getStrategicPlan(vo.getStrategicPlanId(), sessao);
		strategicPlan.setDataCriacao(vo.getDataCriacao());
		strategicPlan.setDataFim(vo.getDataFim());
		strategicPlan.setNome(vo.getNome());
		strategicPlan.setDescricao(vo.getDescricao());
		
		OrganizationElement oe = (OrganizationElement) sessao.load(OrganizationElement.class, vo.getOrganizacaoId());
		strategicPlan.setOrganizacao(oe);
		
		Transaction tx = sessao.beginTransaction();
		
		try{
			sessao.update(strategicPlan);
			tx.commit();
			sessao.close();
		}
		catch(Exception e){
			e.printStackTrace();
			tx.rollback();
			sessao.close();
		}
	}
	
	public StrategicPlan getStrategicPlan(int id, Session sessao){
		StrategicPlan sp = (StrategicPlan) sessao.load(StrategicPlan.class, id);
		return sp;
	}
	
	public StrategicPlanVO getStrategicPlan(int id){
		Session sessao = HibernateUtil.getSession();
		StrategicPlan sp = (StrategicPlan) sessao.load(StrategicPlan.class, id);
		
		StrategicPlanVO vo = new StrategicPlanVO();
		vo.setNome(sp.getNome());
		vo.setDescricao(sp.getDescricao());
		vo.setDataCriacao(sp.getDataCriacao());
		vo.setDataFim(sp.getDataFim());
		vo.setNomeOrganizacao(sp.getOrganizacao().getName());
		vo.setDataCriacaoString(Util.convertDateToString(sp.getDataCriacao()));
		vo.setDataFimString(Util.convertDateToString(sp.getDataFim()));
		vo.setOrganizacaoId(sp.getOrganizacao().getId());
		vo.setStrategicPlanId(sp.getId());
		
		return vo;
	}
}
